package com.tma.mobilechart.line;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLES10;

public class LineAxisCoord3D {

	private FloatBuffer vertexBuffer;	// buffer holding the vertices
	private FloatBuffer colorBuffer;	// buffer holding the color
	private ByteBuffer indexBuffer;		// buffer holding the index

	private float vertices[] = {
            -0.5f, -0.5f,  0.0f,		//0
            -0.5f,  0.5f,  0.0f,		//1
            -0.5f,  0.5f, -0.5f,		//2
            -0.5f, -0.5f, -0.5f,		//3             
             1.75f,-0.5f, -0.5f,		//4
             1.75f,-0.5f,  0.0f			//5
    		};
	
	private float colors[] = {
			0.0f,  1.0f,  0.0f,  1.0f,	//0
            0.0f,  1.0f,  0.0f,  1.0f,	//1
            0.0f,  0.0f,  0.0f,  1.0f,	//2
            0.0f,  0.0f,  0.0f,  1.0f,	//3            
            0.0f,  0.0f,  0.0f,  1.0f,	//4
            0.0f,  0.0f,  0.0f,  1.0f	//5
            };
	
	private byte indices[] = {
            0, 1, 2,    0, 2, 3,
            0, 3, 4,    4, 5, 0
			};
	

	public LineAxisCoord3D() {
		// a float has 4 bytes so we allocate for each coordinate 4 bytes
		ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
		vertexByteBuffer.order(ByteOrder.nativeOrder());
		vertexBuffer = vertexByteBuffer.asFloatBuffer();
		vertexBuffer.put(vertices);
		vertexBuffer.position(0);
		
		//color		
				ByteBuffer colorByteBuffer = ByteBuffer.allocateDirect(colors.length * 4);
				colorByteBuffer.order(ByteOrder.nativeOrder());
				colorBuffer = colorByteBuffer.asFloatBuffer();		
				colorBuffer.put(colors);
				colorBuffer.position(0);
				
		//
		indexBuffer = ByteBuffer.allocateDirect(indices.length);
		indexBuffer.put(indices);
		indexBuffer.position(0);
				
	}
	
	public void draw(GL10 gl) {
		
		//Set the face rotation
		gl.glFrontFace(GL10.GL_CW);
		
		// Point to our vertex buffer
		gl.glVertexPointer(3, GLES10.GL_FLOAT, 0, vertexBuffer);	
		gl.glColorPointer(4, GLES10.GL_FLOAT, 0, colorBuffer);
		
		//Enable the vertex and color state
		gl.glEnableClientState(GLES10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GLES10.GL_COLOR_ARRAY);
		
		// set the colour 
		//gl.glColor4f(0.0f, 1.0f, 0.0f, 0.5f);		

		// Draw the vertices
		//gl.glDrawArrays(GLES10.GL_LINE_STRIP, 0, vertices.length / 3);
		gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 12, GL10.GL_UNSIGNED_BYTE, indexBuffer);

		//Disable the client state before leaving
		gl.glDisableClientState(GLES10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GLES10.GL_COLOR_ARRAY);
	}
}
